﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Auxiliaries;
using System.IO;

namespace CommLink
{
    /// <summary>
    /// Form for communication log output
    /// </summary>
    public partial class CommLog : Form
    {
        /// <summary>
        /// Delegate used for invoking the UI thread when pasting text to the text box
        /// </summary>
        /// <param name="text">Text to paste</param>
        delegate void TextBoxUpdate(string text);

        int ligLen = 50;

        /// <summary>
        /// Public constructor
        /// </summary>
        public CommLog()
        {
            InitializeComponent();

            if (Properties.Settings.Default.CommLog_history_length <= 0)
            {
                Properties.Settings.Default.CommLog_history_length = 50;
                Properties.Settings.Default.Save();
            }

            ligLen = Properties.Settings.Default.CommLog_history_length;
        }
        /// <summary>
        /// Updates the rich text box invoking the UI thread if necessary
        /// </summary>
        /// <param name="text">String to paste to the text box</param>
        void LogTextBoxUpdate(string text)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new TextBoxUpdate(LogTextBoxUpdate), new object[] { text });
            }
            else
            {
                if(tabTreeLogging.SelectedIndex != 1)
                    tabTreeLogging.TabPages[1].BackColor = Color.Blue;

                if (rtbCommLog.Lines.Length > ligLen)
                    rtbCommLog.Lines = rtbCommLog.Lines.Skip(1).ToArray();
                rtbCommLog.AppendText(text);
                rtbCommLog.ScrollToCaret();
            }
        }
        /// <summary>
        /// Appends a log entry on a separate line to the log output textbox
        /// </summary>
        /// <param name="logLine">Log entry</param>
        public void LogLineEntry(string logLine)
        {
            LogTextBoxUpdate(logLine + "\n");
        }
        /// <summary>
        /// Appends a log entry to the log output textbox
        /// </summary>
        /// <param name="logEntry">Log entry</param>
        public void LogEntry(string logEntry)
        {
            LogTextBoxUpdate(logEntry);
        }
        /// <summary>
        /// Appends a log entry for a byte array on a separate line to the log output textbox
        /// </summary>
        /// <param name="logByteLine">Log entry</param>
        public void LogLineEntry(byte[] logByteLine)
        {
            LogTextBoxUpdate(HEX_converter.ToHexStringSpaced(logByteLine) + "\n");
        }
        /// <summary>
        /// Appends a log entry for a byte array to the log output textbox
        /// </summary>
        /// <param name="logByteEntry">Log byte entry</param>
        public void LogEntry(byte[] logByteEntry)
        {
           LogTextBoxUpdate(HEX_converter.ToHexStringSpaced(logByteEntry));
        }

        void ErrorTextBoxUpdate(string text)
        {
            if (InvokeRequired)
            {
                BeginInvoke(new TextBoxUpdate(ErrorTextBoxUpdate), new object[] { text });
            }
            else
            {
                if (tabTreeLogging.SelectedIndex != 0)
                    tabTreeLogging.TabPages[0].BackColor = Color.Blue;

                if (rtbErrorLog.Lines.Length > ligLen)
                    rtbErrorLog.Lines = rtbErrorLog.Lines.Skip(1).ToArray();
                rtbErrorLog.AppendText(text);
                rtbErrorLog.ScrollToCaret();
            }
        }
        /// <summary>
        /// Appends a log entry on a separate line to the log output textbox
        /// </summary>
        /// <param name="logLine">Log entry</param>
        public void ErrorLineEntry(string logLine)
        {
            ErrorTextBoxUpdate(logLine + "\n");
        }
        /// <summary>
        /// Appends a log entry to the log output textbox
        /// </summary>
        /// <param name="logEntry">Log entry</param>
        public void ErrorEntry(string logEntry)
        {
            ErrorTextBoxUpdate(logEntry);
        }
        
        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*";
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                try
                {
                    StreamWriter tWr = new StreamWriter((new FileInfo(sfd.FileName)).Create());
                    tWr.Write(rtbCommLog.Text);
                    tWr.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }

        private void tabTreeLogging_SelectedIndexChanged(object sender, EventArgs e)
        {
            tabTreeLogging.TabPages[tabTreeLogging.SelectedIndex].BackColor = Form.DefaultBackColor;
        }
    }
}
